/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.modelo.Autor;
import mx.its.dsw2.biblioteca.modelo.Libro;

/**
 *
 * @author Maq.8
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
//---------------------Llamadas EJB--------------------------------------

    @EJB
    private AutorBlLocal autorBl;
//---------------------Variables----------------------------
    private List<Autor> authorList;
    private Autor author;
    private String accion;

    /**
     * Creates a new instance of AuthorBean
     */
    public AuthorBean() {
        author = new Autor();
    }
//---------------------------GETTERS y SETTERS---------------------------

    public List<Autor> getAuthorLibro() {
        authorList = autorBl.getAll();
        return authorList;
    }

    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }
//---------------------------Acciones----------------------------
    //Se utilizan cuando hay procesamiento de datos (modificar)si se hace un listener
    //y se cambia un dato es probable que no se tome en cuenta

    public String procesarAction() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            autorBl.register(author);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                autorBl.modify(author);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    autorBl.delete(author);
                }
            }
        }

        accion = "";
        return "AuthorList";
    }
    
      public String prepararCancelar(){
        String regresa = "";
        if (accion.equalsIgnoreCase("Eliminar")) {
           regresa = "AuthorList";
           
        } if(accion.equalsIgnoreCase("Editar")|| accion.equalsIgnoreCase("Nuevo")){
            regresa = "AuthorCrearEditar";
        }
        return regresa;
    }

//--------------------------Listeners----------------------------
    public void prepararNuevo(ActionEvent e) {//javax.faces.event.ActionEvent
        accion = "Nuevo";
        author = new Autor();
    }

    public void prepararEditar(ActionEvent e) {
        accion = "Editar";
        /*System.out.println(e.getComponent().getClientId());
         String arr[]= e.getComponent().getClientId().split(":");
         String id = arr[2];
         int index = Integer.parseInt(id);*/

        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        author = authorList.get(index);
    }

    public void prepararEliminar(ActionEvent e) {
        accion = "Eliminar";
        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        author = authorList.get(index);
    }

  
}
